﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using log4net;
using Zinnia.Kernel.Core;

namespace Zinnia.Log
{
    /// <summary>
    /// 日志服务
    /// </summary>
    public class LogService : ILogService
    {
        IDictionary<string, ILog> logDic = new ConcurrentDictionary<string, ILog>();
        private string _name;
        object objlock = new object();
        private ILog Current => logDic[_name];
        internal LogService(string name)
        {
            _name = string.IsNullOrEmpty(name) ? "zinnia" : name;
            if (!logDic.ContainsKey(_name))
            {
                lock (objlock)
                {
                    if (!logDic.ContainsKey(_name))
                    {
                        logDic.Add(_name, LogManager.GetLogger(_name));
                    }
                }
            }
        }

        public void Debug(string message, Exception ex)
        {
            Current.Debug($"msg:{message},exp:{ex}");
        }

        public void Error(string message, Exception ex)
        {
            Current.Error($"msg:{message},exp:{ex}");
        }

        public void Info(string message, Exception ex)
        {
            Current.Info($"msg:{message},exp:{ex}");
        }
    }

    public class LogManagerService : ILogManagerService
    {
        public ILogService GetLogger(string name)
        {
            return new LogService(name);
        }
    }
}
